package org.txazo.blog.web.action.base;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.txazo.blog.util.HttpUtil;
import org.txazo.blog.vo.MemberInfo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * BaseAction
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-10
 * 
 */
@Namespace("/")
@ParentPackage(value = "base")
@InterceptorRefs({ @InterceptorRef("default") })
@ExceptionMappings({ @ExceptionMapping(exception = "java.lang.Exception", result = "error") })
@Results({
		@Result(name = "index", type = "redirect", location = "/index.jsps"),
		@Result(name = "login", type = "redirect", location = "/login/index.jsps"),
		@Result(name = "error", type = "redirect", location = "/common/error.jsps"),
		@Result(name = "ip", type = "redirect", location = "/common/ip.jsps") })
public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	/** 用户ID */
	private Integer memberId;
	/** 用户实体 */
	private MemberInfo memberInfo;
	/** 错误提示信息 */
	protected String errorMessage;
	/** Action请求URL */
	protected String url;

	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;
	protected HttpSession session = null;
	protected ServletContext application = null;

	public BaseAction() {
		super();

		init();
	}

	private void init() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		session = request.getSession();
		application = session.getServletContext();

		url = HttpUtil.getRequestURLIncludeParameters(request);

		memberInfo = (MemberInfo) session.getAttribute("memberInfo");
		if (memberInfo != null) {
			memberId = memberInfo.getMemberId();
		}
	}

	protected Integer getLoginMemberId() {
		return memberId;
	}

	protected MemberInfo getLoginMemberInfo() {
		return memberInfo;
	}

	protected String getRemoteIP() {
		return HttpUtil.getRemoteIp(request);
	}

	protected HttpServletRequest getRequest() {
		return request;
	}

	protected HttpServletResponse getResponse() {
		return response;
	}

	protected HttpSession getSession() {
		return session;
	}

	protected ServletContext getApplication() {
		return application;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public String getUrl() {
		return url;
	}

}
